#!/usr/bin/env python
# -*- coding: iso-8859-15 -*-
# Martin Mason on Wed Nov 11 21:28:01 2009

import wx
from CRC import * #import the Checksum functions
from BioloidHighLevelCmds import *

# begin wxGlade: extracode
# end wxGlade



class PoseEditor(wx.Frame):
    def __init__(self, *args, **kwds):
        # begin wxGlade: PoseEditor.__init__
        kwds["style"] = wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__(self, *args, **kwds)
        self.updateValues = wx.Button(self, -1, "Update Values")
        self.savePosition = wx.Button(self, -1, "Save Position")
        self.playPose = wx.Button(self, -1, "Play Pose")
        self.releaseMotor = wx.ToggleButton(self, -1, "Release Motor")
        self.motorNumber = wx.SpinCtrl(self, -1, "", min=0, max=21)
        self.moveMotor = wx.Button(self, -1, "Move Motor")
        self.poseStatus = wx.TextCtrl(self, -1, "", style=wx.TE_MULTILINE|wx.TE_WORDWRAP)
        self.motorPosition = wx.SpinCtrl(self, -1, "", min=0, max=100)
        self.Comm_Port = wx.SpinCtrl(self, -1, "", min=0, max=100)
        self.connectComms = wx.Button(self, -1, "Connect")
        self.fileName = wx.TextCtrl(self, -1, "")
        self.createFile = wx.Button(self, -1, "Create File")
        self.terminateAll = wx.Button(self, -1, "Exit")

        self.__set_properties()
        self.__do_layout()

        self.Bind(wx.EVT_BUTTON, self.update_values, self.updateValues)
        self.Bind(wx.EVT_BUTTON, self.save_position, self.savePosition)
        self.Bind(wx.EVT_BUTTON, self.play_pose, self.playPose)
        self.Bind(wx.EVT_TOGGLEBUTTON, self.release_motor, self.releaseMotor)
        self.Bind(wx.EVT_BUTTON, self.move_motor, self.moveMotor)
        self.Bind(wx.EVT_BUTTON, self.connect_comms, self.connectComms)
        self.Bind(wx.EVT_BUTTON, self.create_file, self.createFile)
        self.Bind(wx.EVT_BUTTON, self.terminate_all, self.terminateAll)
        # end wxGlade

    def __set_properties(self):
        # begin wxGlade: PoseEditor.__set_properties
        self.SetTitle("Pose Editor")
        self.motorNumber.SetMinSize((80, -1))
        self.poseStatus.SetMinSize((160, 60))
        self.motorPosition.SetMinSize((80, -1))
        self.Comm_Port.SetMinSize((80, -1))
        # end wxGlade

    def __do_layout(self):
        # begin wxGlade: PoseEditor.__do_layout
        sizer_1 = wx.BoxSizer(wx.VERTICAL)
        grid_sizer_1 = wx.FlexGridSizer(4, 3, 1, 1)
        sizer_2 = wx.BoxSizer(wx.VERTICAL)
        grid_sizer_1.Add(self.updateValues, 0, 0, 0)
        grid_sizer_1.Add(self.savePosition, 0, 0, 0)
        grid_sizer_1.Add(self.playPose, 0, 0, 0)
        grid_sizer_1.Add(self.releaseMotor, 0, 0, 0)
        grid_sizer_1.Add(self.motorNumber, 0, 0, 0)
        grid_sizer_1.Add(self.moveMotor, 0, 0, 0)
        grid_sizer_1.Add(self.poseStatus, 0, 0, 0)
        grid_sizer_1.Add(self.motorPosition, 0, 0, 0)
        sizer_2.Add(self.Comm_Port, 0, 0, 0)
        sizer_2.Add(self.connectComms, 0, 0, 0)
        grid_sizer_1.Add(sizer_2, 1, wx.EXPAND, 0)
        grid_sizer_1.Add(self.fileName, 0, wx.EXPAND, 0)
        grid_sizer_1.Add(self.createFile, 0, 0, 0)
        grid_sizer_1.Add(self.terminateAll, 0, 0, 0)
        sizer_1.Add(grid_sizer_1, 1, wx.EXPAND, 0)
        self.SetSizer(sizer_1)
        sizer_1.Fit(self)
        self.Layout()
        # end wxGlade

    def update_values(self, event): # wxGlade: PoseEditor.<event_handler>
        global output_data
        current_position = getServos()
        self.poseStatus.WriteText(str(current_position)+chr(13))
        output_data.append(str(current_position))

    def save_position(self, event): # write all positions to file
        global output_data,filename
        PoseControl.poseStatus.WriteText(str(output_data)+chr(13))
        PoseControl.poseStatus.WriteText("Writing File")
        try:
            FILE = open(filename,"w")
            FILE.writelines(output_data)
            FILE.close()
            PoseControl.poseStatus.WriteText("Write OK")
        except:
            PoseControl.poseStatus.WriteText("Write Failed")


    def play_pose(self, event): # wxGlade: PoseEditor.<event_handler>
        for i in range(0,6):
            addCRCandSend("P"+"000"+str(i)+"0000"+"0000")
            PoseControl.poseStatus.WriteText("Pose #"+str(i)+chr(13))
            time.sleep(1.4)
            
    def release_motor(self, event): # wxGlade: PoseEditor.<event_handler>
        addCRCandSend("O"+"0000"+"0000"+"0000")
        PoseControl.poseStatus.WriteText("Motors Off"+chr(13))

    def move_motor(self, event): # wxGlade: PoseEditor.<event_handler>
        setServo( self.motorNumber.GetValue(), self.motorPosition.GetValue())
        print "Event handler `move_motor' not implemented!"
        event.Skip()

    def connect_comms(self, event): # wxGlade: PoseEditor.<event_handler>
        print self.Comm_Port.GetValue()
        PoseControl.poseStatus.WriteText("Init Comms "+str(self.Comm_Port.GetValue())+chr(13))
        initComm(self.Comm_Port.GetValue())

    def create_file(self, event): # wxGlade: PoseEditor.<event_handler>
        global output_data,filename #This will contain a list of all the poses
        output_data = [] #Initialize output_data to empty
        posename = self.fileName.GetValue()
        self.poseStatus.WriteText("Creating "+posename+".txt"+chr(13))
        filename = posename+".txt"

    def terminate_all(self, event): # wxGlade: PoseEditor.<event_handler>
        global ser
        try:
            ser.close()
        except:
            print "serial not open"
        PoseControl.Destroy()
        wx.Exit()

# end of class PoseEditor


if __name__ == "__main__":
    app = wx.PySimpleApp(0)
    wx.InitAllImageHandlers()
    PoseControl = PoseEditor(None, -1, "")
    app.SetTopWindow(PoseControl)
    PoseControl.Show()
    app.MainLoop()
